package com.wzy.pipeline2;

import com.alibaba.fastjson.JSON;
import com.wzy.pipeline2.model.FlowResult;
import com.wzy.pipeline2.model.PipeLineContext;
import com.wzy.pipeline2.validator.OrderPreviewVaildator;
import com.wzy.pipeline2.processor.Processor;
import com.wzy.pipeline2.validator.Validator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: wzy
 * @Date: 2021/12/11/16:51
 * @Description:
 */
@RestController
@Slf4j
public class SimpleControllerTest {

    @Autowired
    private NormalPipeLine normalPipeLine;
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private Validator validator;
    @Autowired
    private OrderPreviewVaildator orderPreviewValidator;
    @Autowired
    private Processor processor;

    @RequestMapping("/pipe/add")
    public FlowResult testUserController() {
        NormalPipeLine normalPipeLine = (NormalPipeLine) applicationContext.getBean("normalPipeLine");
        // 定义上下文
        PipeLineContext pipeLineContext = new PipeLineContext(0);
        pipeLineContext.put("index", "0");
        // 增加阀门
        normalPipeLine.addValve(validator); // 参数校验阀门
        normalPipeLine.addValve(orderPreviewValidator); // 业务校验阀门
        normalPipeLine.addValve(processor); // 业务处理阀门
        // 管道执行
        FlowResult flowResult = normalPipeLine.start(pipeLineContext);
        log.info(JSON.toJSONString(flowResult));
        return flowResult;
    }
}
